<?php
if (!defined('PLUGINFRAME'))exit('hacking attempt'); 
class admin extends common{
    
    private $_cates=array();
    
    public function __construct(){
        parent::__construct();
        $this->assgin('title','臭美文化管理后台——Android');
    }
    
    public function index(){//openid FAEAB4541899786105AE01FA34EF184F

        $this->display();
    }   
    
    public function add(){
        $con='';
        $s=isset($_REQUEST['s'])?$_REQUEST['s']:'';
        $e=isset($_REQUEST['e'])?$_REQUEST['e']:'';
        if ( $s && $e ){
            $pid=$this->getItemId($s, $e);
            $con_mod=D('Content');
            $pid && $con=$con_mod->select(array(
                'where'=>array('is_del'=>0,'item_id'=>$pid),
                'limit'=>false,
            ));
        }
        $this->assgin('s', $s);
        $this->assgin('e', $e);
        $this->assgin('content', $con);
        $this->display();
    }
    
    public function getItemId($sd,$ed){
        $item_mod=D('Item'); 
        $sql='select id from '.$item_mod->table().' where sdate='.strtotime($sd).' and edate='.strtotime($ed);
        if ($pRes=$item_mod->query($sql)){
            $pid=mysql_fetch_row($pRes);
            return $pid[0];
        }
        return false;
    }
    public function submit(){
        $data=empty($_REQUEST['data'])?array():array_slice($_REQUEST['data'],1);
        $sdate=$_REQUEST['sdate'];
        $edate=$_REQUEST['edate'];
        //$status=false;
        //if (is_numeric($pid)) {
            $con_mod=D('Content');
            $item_mod=D('Item');
            $pid=$this->getItemId($sdate,$edate);         
            if (!$pid){
                $inSql='insert into '.$item_mod->table().'(`sdate`,`edate`,`cre_date`)value('.strtotime($sdate).','.strtotime($edate).','.time().')';
                $item_mod->query($inSql);
            }
            $sql='insert into '.$con_mod->table().'(`item_id`,`con`,`publish`,`leader`,`ad`,`note`,`cre_time`)value';

            foreach ($data as $dKey=>$dVal){
              $itemId=array_shift($dVal);//新增的开始,最后一个元素标记是否新增,0新增1修改
              if (is_numeric($itemId) && 0==$itemId){
                $str='('.$this->getItemId($sdate,$edate);
                foreach ($dVal as $ddKey=>$ddVal){
                    switch ($ddKey){
                        case 1:
                            $str.=','.strtotime($ddVal);
                            break;
                        case 2:
                        case 3:
                            $str.=','.$ddVal;
                            break;
                        case 0:
                        case 4:
                            $str.=',\''.$ddVal.'\'';
                            break;
                    }
                }
                $str.=','.time().'),';
                $sql.=$str;
              }/*else{//新增的结束,更新的开始
                $upSql.='update '.$con_mod->table().' set ';
                foreach ($dVal as $ddKey=>$ddVal){
                    switch ($ddKey){
                        case 1:
                            $upSql.=' publish='.strtotime($ddVal).',';
                            break;
                        case 2:
                            $upSql.=' leader='.$ddVal.',';
                            break;
                        case 3:
                            $upSql.=' ad='.$ddVal.',';
                            break;
                        case 0:
                            $upSql.=' con=\''.$ddVal.'\',';
                            break;
                        case 4:
                            $upSql.=' note=\''.$ddVal.'\';';
                            break;
                    }
                }
              }//更新结束*/
            }
            if (!empty($str)){
                $sql=trim($sql,',');
                $con_mod->query($sql);
            }
        echo $sdate.'|'.$edate;
    }
    
    public function rebuilt(){
        $s=$_GET['s'];
        $e=$_GET['e'];
        $pid=$this->getItemId($s,$e);
        if (is_numeric($pid)) {
            $con_mod=D('Content');
            $res=$con_mod->query('select item_id,con,publish,leader,ad,note from '.$con_mod->table().' where is_del=0 and item_id='.$pid);
            $rbRes=array();
            while ($row=mysql_fetch_row($res))
                $rbRes[]=$row;
            $rbStr='';
            foreach ($rbRes as $rbVal){
                $rbVal[2]=date('Y-m-d H:i:s',$rbVal[2]);
                switch ($rbVal[3]){
                    case 1:$rbVal[3]='华敏';break;
                    case 2:$rbVal[3]='李辉川';break;
                    case 3:$rbVal[3]='沈立昀';break;
                    case 4:$rbVal[3]='邹师';break;
                    case 5:$rbVal[3]='袁永高';break;
                    case 6:$rbVal[3]='黄春刚';break;
                }
                $rbVal[4]=$rbVal[4]==1?'有':'没有';
                $rbStr.=implode($rbVal, ',').'|';
            }
            echo rtrim($rbStr,'|');
        }
    }

    //not in productId limit分页不行
    public function update(){
        $data=$_GET['data'];//exit(var_export($data,true));
        if ($data){
            $con_mod=D('Content');
            $sql='update '.$con_mod->table().' set con="'.$data[1].'",publish='.strtotime($data[2]).',leader='.$data[3].',ad='.$data[4].',note="'.$data[5].'" where id='.$data[0];
            //exit($sql);
            $con_mod->query($sql);
            echo 'ok';
        }
    }
    
    public function del(){
        $pk=$_GET['pk'];
        $mod=isset($_GET['item'])?D('Item'):D('Content');
        $mod->query('update '.$mod->table().' set is_del=1 where id='.$pk);
        if (isset($_GET['item'])){
            $con_mod=D('Content');
            $con_mod->query('update '.$con_mod->table().' set is_del=1 where item_id='.$pk);
        }
        echo 'ok';
    }
}